package com.tarena.lbs.service.impl;

import com.tarena.lbs.base.exception.ServiceException;
import com.tarena.lbs.base.response.StatusCode;
import com.tarena.lbs.mapper.AdminMapper;
import com.tarena.lbs.dto.AdminLoginParam;
import com.tarena.lbs.dto.AdminParam;
import com.tarena.lbs.dto.AdminQuery;
import com.tarena.lbs.entity.Admin;
import com.tarena.lbs.vo.AdminVO;
import com.tarena.lbs.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public void insert(AdminParam adminParam) {
        log.debug("账户保存参数:{}",adminParam);
        Admin admin=new Admin();
        BeanUtils.copyProperties(adminParam,admin);
        if (admin.getId()==null){
            admin.setCreateTime(new Date());
            adminMapper.insert(admin);
        }else {
            adminMapper.update(admin);
        }
    }

    @Override
    public List<AdminVO> selectAdmin(AdminQuery adminQuery) {
        log.debug("后台账户列表查询参数:{}",adminQuery);
        List<AdminVO> list=adminMapper.selectAdmin(adminQuery);
        return list;
    }

    @Override
    public void delete(Integer id) {
        log.debug("删除后台用户请求:{}",id);
        adminMapper.deleteAdmin(id);
    }

/*    @Override
    public AdminVO AdminLogin(AdminLoginParam adminLoginParam) {
        log.debug("管理员登录请求:{}",adminLoginParam);
        AdminVO adminVO=adminMapper.AdminLogin(adminLoginParam);
        System.out.println(adminVO);
        if (adminVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!adminVO.getAccountPassword().equals(adminLoginParam.getAccountPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }

        return adminVO;
    }*/
@Override
public AdminVO AdminLogin(AdminLoginParam adminLoginParam) {
    log.debug("管理员登录请求:{}", adminLoginParam);

    // 参数校验
    if (adminLoginParam == null ||
            adminLoginParam.getNickname() == null ||
            adminLoginParam.getAccountPassword() == null) {
        throw new ServiceException(StatusCode.USERNAME_ERROR);
    }

    AdminVO adminVO = adminMapper.AdminLogin(adminLoginParam);
    log.debug("查询结果: {}", adminVO);

    if (adminVO == null) {
        throw new ServiceException(StatusCode.USERNAME_ERROR);
    }

    // 此处应改为使用数据库中的密码进行验证
    // 但根据你的SQL，密码已经在查询条件中验证过了
    // 这里可以添加额外的验证逻辑或移除重复验证

    return adminVO;
}

}

